package com.wanjia.estate.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

public class BarcodeGeneratorUtil {
    public static void getBarCode(String msg, String filePath) throws IOException {
        String barcodeText = msg;
        BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
        int width = 500;
        int height = 100;

        // 设置编码参数
        Code128Writer writer = new Code128Writer();
        BitMatrix bitMatrix = writer.encode(barcodeText, barcodeFormat, width, height);

        // 保存为PNG格式的图片
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

    }
}
